package com.kgc.itrip.service.impl;

import com.kgc.itrip.common.Constants;
import com.kgc.itrip.common.DtoUtil;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.common.Page;
import com.kgc.itrip.dto.Dto;
import com.kgc.itrip.mapper.ItripUserLinkUserMapper;
import com.kgc.itrip.pojo.ItripUser;
import com.kgc.itrip.pojo.ItripUserLinkUser;
import com.kgc.itrip.response.ResultCode;
import com.kgc.itrip.service.ItripUserLinkUserService;
import com.kgc.itrip.service.TokenService;
import com.kgc.itrip.vo.ItripAddUserLinkUserVO;
import com.kgc.itrip.vo.ItripSearchUserLinkUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItripUserLinkUserServiceImpl implements ItripUserLinkUserService {

    @Resource
    private TokenService tokenService;
    @Resource
    private ItripUserLinkUserMapper itripUserLinkUserMapper;

    public ItripUserLinkUser getItripUserLinkUserById(Long id)throws Exception{
        return itripUserLinkUserMapper.getItripUserLinkUserById(id);
    }

    public List<ItripUserLinkUser>	getItripUserLinkUserListByMap(Map<String,Object> param)throws Exception{
        return itripUserLinkUserMapper.getItripUserLinkUserListByMap(param);
    }

    public Integer getItripUserLinkUserCountByMap(Map<String,Object> param)throws Exception{
        return itripUserLinkUserMapper.getItripUserLinkUserCountByMap(param);
    }

    public Integer itriptxAddItripUserLinkUser(ItripUserLinkUser itripUserLinkUser)throws Exception{
            itripUserLinkUser.setCreationDate(new Date());
            return itripUserLinkUserMapper.insertItripUserLinkUser(itripUserLinkUser);
    }

    public Integer itriptxModifyItripUserLinkUser(ItripUserLinkUser itripUserLinkUser)throws Exception{
        itripUserLinkUser.setModifyDate(new Date());
        return itripUserLinkUserMapper.updateItripUserLinkUser(itripUserLinkUser);
    }

    public Integer itriptxDeleteItripUserLinkUserById(Long id)throws Exception{
        return itripUserLinkUserMapper.deleteItripUserLinkUserById(id);
    }

    public Page<ItripUserLinkUser> queryItripUserLinkUserPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = itripUserLinkUserMapper.getItripUserLinkUserCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripUserLinkUser> itripUserLinkUserList = itripUserLinkUserMapper.getItripUserLinkUserListByMap(param);
        page.setRows(itripUserLinkUserList);
        return page;
    }


    @Override
    public Dto addItripUserLinkUser(String agent,String token, ItripAddUserLinkUserVO itripAddUserLinkUserVO) throws Exception {
        //查询token是否失效
        if(!tokenService.validate(agent,token)){//token无效的情况
            return DtoUtil.returnFail(ResultCode.TOKEN_TIMEOUT.getMsg(),
                    ResultCode.TOKEN_TIMEOUT.getCode());
        }
        //根据token获取对应用户信息：
        ItripUser itripUser = tokenService.load(token);
        //获取当前用户id
        String userid = itripUser.getId().toString();
        //补全itripAddUserLinkUserVO信息
        itripAddUserLinkUserVO.setUserId(itripUser.getId());
        //判断itripAddUserLinkUserVO是否为空
        if(itripAddUserLinkUserVO==null){
            return DtoUtil.returnFail(ResultCode.LINKUSER_IS_NULL.getMsg(),
                    ResultCode.LINKUSER_IS_NULL.getCode());
        }
        //创建添加对象：
        ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
        BeanUtils.copyProperties(itripAddUserLinkUserVO,itripUserLinkUser);
        //补全itripUserLinkUser的信息
        itripUserLinkUser.setUserId(Integer.valueOf(userid));
        itripUserLinkUser.setCreatedBy(itripUser.getId());
        Integer num = itriptxAddItripUserLinkUser(itripUserLinkUser);
        if(num>0){
            return DtoUtil.returnSuccess();
        }
        return DtoUtil.returnFail(ResultCode.ADD_LINKUSER_ERROR.getMsg(),
                ResultCode.ADD_LINKUSER_ERROR.getCode());
        /*Map<String,Object> prams = new HashMap<>();
        prams.put("linkUserName",itripAddUserLinkUserVO.getLinkUserName());
        prams.put("linkIdCard",itripAddUserLinkUserVO.getLinkIdCard());
        prams.put("linkPhone",itripAddUserLinkUserVO.getLinkPhone());
        prams.put("userId",itripUser.getId());
        prams.put("linkIdCardType",itripAddUserLinkUserVO.getLinkIdCardType());
        itripUserLinkUserMapper.i*/

    }

    @Override
    public Dto queryUserLinkUser(String agent,
                                 String token,
                                 ItripSearchUserLinkUserVO itripSearchUserLinkUserVO, Integer pageNo, Integer pageSize) throws Exception {
        //查询token是否失效
        if(!tokenService.validate(agent,token)){//token无效的情况
            return DtoUtil.returnFail(ResultCode.TOKEN_TIMEOUT.getMsg(),
                    ResultCode.TOKEN_TIMEOUT.getCode());
        }
        //获取查询条件
        ItripUser itripUser = tokenService.load(token);
        String linkUserName = itripSearchUserLinkUserVO.getLinkUserName();
        Map<String,Object> param = new HashMap<>();
        param.put("userId",itripUser.getId());
        param.put("linkUserName",linkUserName);
        Page<ItripUserLinkUser> linkUserPage = queryItripUserLinkUserPageByMap(param, pageNo, pageSize);
        return DtoUtil.returnSuccess("查询成功",linkUserPage.getRows());

    }

}
